package com.zlll.winner.service.bbs;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.zlll.winner.business.model.bbs.BbsUserActionLog;
import com.zlll.winner.business.service.bbs.IBbsPostService;
import com.zlll.winner.business.service.bbs.IBbsUserCommentService;
import com.zlll.winner.business.vo.bbs.BbsPostVo;
import com.zlll.winner.business.vo.bbs.BbsUserActionLogVo;
import com.zlll.winner.business.vo.bbs.BbsUserCommentVo;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.bbs.ActionLogEnum;
import com.zlll.winner.mapper.bbs.BbsUserActionLogMapper;
import com.zlll.winner.business.service.bbs.IBbsUserActionLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.service.bbs.async.BbsCommentAsyncService;
import com.zlll.winner.service.bbs.async.BbsPostAsyncService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 * 论坛用户点赞转发操作记录表 服务实现类
 * </p>
 *
 * @author Walert
 * @since 2019-09-12
 */
@Service
public class BbsUserActionLogServiceImpl extends ServiceImpl<BbsUserActionLogMapper, BbsUserActionLog> implements IBbsUserActionLogService {

    @Autowired
    private BbsPostAsyncService postAsyncService;

    @Autowired
    private BbsCommentAsyncService commentAsyncService;

    @Autowired
    private IBbsPostService postService;

    @Autowired
    private IBbsUserCommentService commentService;

    /**
     * 保存用户操作日志
     *
     * @param params
     * @return
     */
    @Override
    public Object saveLog(JSONObject params) throws InterruptedException {

        BbsUserActionLog saveObj = new BbsUserActionLog();

        Long bizId = params.getLong("bizId");
        Integer bizType = params.getInteger("bizType");
        Integer actionType = params.getInteger("actionType");
        Long userId = params.getLong("userId");
        String userName = params.getString("userName");
        String userImg = params.getString("userImg");

        Long actionBizId = params.getLong("actionBizId");
        Integer actionBizType = params.getInteger("actionBizType");


        saveObj.setBizId(bizId);
        saveObj.setBizType(bizType);
        saveObj.setActionType(actionType);
        saveObj.setUserId(userId);
        saveObj.setUserName(userName);
        saveObj.setUserImg(userImg);
        saveObj.setActionBizId(actionBizId);
        saveObj.setActionBizType(actionBizType);
        saveObj.setCreateTime(System.currentTimeMillis());
        saveObj.setUpdateTime(saveObj.getCreateTime());

        baseMapper.insert(saveObj);

        if (bizType != null && bizType == ActionLogEnum.BizTypeEnum.POST.val()) {
            postAsyncService.changeNumber(bizId, actionType);
        } else if (bizType != null && bizType == ActionLogEnum.BizTypeEnum.COMMENT.val()) {
            if(!actionType.equals(ActionLogEnum.ActionTypeEnum.REMOVE.val())){
                commentAsyncService.changeNumber(bizId, actionType);
            }
        }
        return saveObj;
    }

    public List<BbsUserActionLog> getBbsUserActionLogs(Long bizId, Integer bizType, Long userId,Integer actionType) {
        return baseMapper.selectList(
                        Wrappers.<BbsUserActionLog>lambdaQuery()
                                .eq(BbsUserActionLog::getBizId, bizId)
                                .eq(BbsUserActionLog::getBizType, bizType)
                                .eq(BbsUserActionLog::getActionType,actionType)
                                .eq(BbsUserActionLog::getUserId, userId)
                                .eq(BbsUserActionLog::getIsDel, CommonEnum.DelEnum.UNDEL.val()));
    }

    /**
     * @param params
     * @return
     */
    @Override
    public Object getLogByUserId(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        IPage<BbsUserActionLogVo> page = new Page<>(pageNo, pageSize);
        QueryWrapper<BbsUserActionLogVo> queryWrapper = new QueryWrapper<>();

        Long userId = params.getLong("userId");

        IPage<BbsUserActionLogVo> actionLogVoIPage = baseMapper.selectPageByCustomer(page, queryWrapper, userId);
        Set<Long> postIds = new HashSet<>();
        Set<Long> commentIds = new HashSet<>();
        if(actionLogVoIPage.getTotal() > 0) {
            List<BbsUserActionLogVo> records = actionLogVoIPage.getRecords();
            for (int i = 0; i < records.size(); i++) {
                BbsUserActionLogVo bbsUserActionLogVo = records.get(i);
                if(bbsUserActionLogVo.getBizType().equals(ActionLogEnum.BizTypeEnum.POST.val())){
                    postIds.add(bbsUserActionLogVo.getBizId());
                } else if (bbsUserActionLogVo.getBizType().equals(ActionLogEnum.BizTypeEnum.COMMENT.val())) {
                    commentIds.add(bbsUserActionLogVo.getBizId());
                }
                if(bbsUserActionLogVo.getActionBizId() != null){
                    commentIds.add(bbsUserActionLogVo.getActionBizId());
                }
            }
            Map<Long, BbsPostVo> postVoMap = new HashMap<>();
            if(postIds != null && postIds.size() > 0){
                JSONObject params2 = new JSONObject();
                params2.put("postIds",postIds);
                params2.put("userId",userId);
                List<BbsPostVo> postDetailsList = postService.getDetailsList(params2);
                postDetailsList.forEach(item->{ postVoMap.put(item.getId(),item);});
            }
            Map<Long, BbsUserCommentVo> commentVoMap = new HashMap<>();
            if(commentIds != null && commentIds.size() > 0){
                JSONObject params2 = new JSONObject();
                params2.put("commentIds",commentIds);
                params2.put("userId",userId);
                List<BbsUserCommentVo> commentDetailsList = commentService.getDetailsList(params2);
                commentDetailsList.forEach(item->{ commentVoMap.put(item.getId(),item);});

            }
            for (BbsUserActionLogVo vo : records) {
                if (vo.getBizType().equals(ActionLogEnum.BizTypeEnum.POST.val())) {
                    vo.setPost(postVoMap.get(vo.getBizId()));
                    if(vo.getActionBizId()!=null){
                        vo.setComment(commentVoMap.get(vo.getActionBizId()));
                    }
                } else if (vo.getBizType().equals(ActionLogEnum.BizTypeEnum.COMMENT.val())) {
                    vo.setComment(commentVoMap.get(vo.getBizId()));
                    if(vo.getActionBizId()!=null){
                        vo.setReplyComment(commentVoMap.get(vo.getActionBizId()));
                    }
                }
            }
        }
        return actionLogVoIPage;
    }
}
